package cn.afterturn.easypdf.pdf;

import cn.afterturn.easypdf.entity.PdfImageEntity;
import cn.afterturn.easypdf.enums.PdfFont;
import cn.afterturn.easypdf.enums.PdfTitle;
import cn.afterturn.easypdf.pdf.entity.PdfExportParams;
import cn.afterturn.easypdf.pdf.export.PdfExportServer;
import cn.afterturn.easypdf.pdf.styler.IPdfExportStyler;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * pdf 链式导出工具
 *
 * @author JueYue
 * @version 5.0
 * 2024-8-17
 */
public class PdfBuilder {

    private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportServer.class);

    private PDDocument document;

    private PdfExportServer pdfExportServer;

    private PdfExportParams params;

    private PdfBuilder() {

    }

    public static PdfBuilder create() {
        PdfBuilder pdfBuilder = new PdfBuilder();
        pdfBuilder.document = new PDDocument();
        pdfBuilder.pdfExportServer = new PdfExportServer(pdfBuilder.document);
        return pdfBuilder;
    }

    public static PdfBuilder create(PdfExportParams params) {
        PdfBuilder pdfBuilder = new PdfBuilder();
        pdfBuilder.document = new PDDocument();
        pdfBuilder.params = params;
        pdfBuilder.pdfExportServer = new PdfExportServer(pdfBuilder.document);
        return pdfBuilder;
    }

    public PDDocument getDocument() {
        return document;
    }


    public PdfBuilder addPage(PdfExportParams params, boolean newPage) {
        pdfExportServer.addPage(params, newPage);
        return this;
    }

    public PdfBuilder setStyler(IPdfExportStyler styler) {
        pdfExportServer.setStyler(styler);
        return this;
    }

    public void setParams(PdfExportParams params) {
        this.params = params;
    }

    public PdfBuilder addTitle(PdfTitle title, String titleName) {
        pdfExportServer.addTitle(params, title, titleName, Color.BLACK);
        return this;
    }

    public PdfBuilder addTitle(PdfTitle title, String titleName, Color color) {
        pdfExportServer.addTitle(params, title, titleName, color);
        return this;
    }

    public PdfBuilder addLine(PdfFont font, String content) {
        pdfExportServer.addLine(params, content, font.isBold(), font.isItalic(), font.getFontSize(), Color.BLACK);
        return this;
    }

    public PdfBuilder addLine(PdfFont font, String content, Color color) {
        pdfExportServer.addLine(params, content, font.isBold(), font.isItalic(), font.getFontSize(), color);
        return this;
    }

    public PdfBuilder addLine(String content, boolean isBold, boolean isItalic, int fontSize, Color color) {
        pdfExportServer.addLine(params, content, isBold, isItalic, fontSize, color);
        return this;
    }


    public PdfBuilder addTable(PdfExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
        pdfExportServer.addTable(entity, pojoClass, dataSet, false);
        return this;
    }

    public PdfBuilder addTable(PdfExportParams entity, List<ExcelExportEntity> entityList, Collection<? extends Map<?, ?>> dataSet) {
        pdfExportServer.addTable(entity, entityList, dataSet, false);
        return this;
    }

    public PdfBuilder addHeight(float height) {
        pdfExportServer.getPageInfo().setCurrentHeight(pdfExportServer.getPageInfo().getCurrentHeight() + height);
        return this;
    }

    public PdfBuilder addImage(PdfImageEntity image) {
        pdfExportServer.addImage(params, image);
        return this;
    }


    public PdfBuilder addHeightAndStartY(float height) {
        pdfExportServer.getPageInfo().setCurrentHeight(pdfExportServer.getPageInfo().getCurrentHeight() + height);
        pdfExportServer.getPageInfo().setStartY(pdfExportServer.getPageInfo().getStartY() + height);
        return this;
    }

    public PdfBuilder addStartX(float width) {
        pdfExportServer.getPageInfo().setStartX(pdfExportServer.getPageInfo().getStartX() + width);
        return this;
    }

    public PdfBuilder close() {
        IOUtils.closeQuietly(pdfExportServer.getPageInfo().getContentStream());
        return this;
    }

    public PdfExportParams getParams() {
        return params;
    }
}
